﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using MHYS.DEMO.Model;

namespace MHYS.DEMO.DAL
{
    public class ProductService
    {


        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns>商品集合</returns>
        public static List<Product> SelectProduct()
        {
            try
            {
                List<Product> result = new List<Product>();
                foreach (DataRow item in DBHelper.SelectDatatable("SELECT * FROM Product").Rows)
                {
                    Product pro = new Product()
                    {
                        Gid = Convert.ToInt32(item["Gid"].ToString()),
                        GName = item["GName"].ToString(),
                        Address = item["Address"].ToString(),
                        ImageUrl = item["ImageUrl"].ToString(),
                        Price = Convert.ToDecimal(item["Price"].ToString())
                    };

                    //添加小图片
                    pro.Image = new List<ProductImage>();
                    foreach (DataRow img in DBHelper.SelectDatatable(string.Format("SELECT * FROM ProductImage WHERE Gid= '{0}'", pro.Gid)).Rows)
                    {
                        pro.Image.Add(new ProductImage
                        {
                            Gid = pro.Gid,
                            Image = img["Image"].ToString(),
                            ImgeId = Convert.ToInt32(img["ImgeID"].ToString())
                        });

                    }
                    result.Add(pro);
                }
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 根据商品编号查询数据
        /// </summary>
        /// <param name="Pid">商品编号</param>
        /// <returns>商品实体数据集</returns>
        public static List<Product> SelectProduct(int Pid)
        {
            string sql = string.Format("SELECT * FROM Product WHERE Gid='{0}'", Pid);
            List<Product> result = new List<Product>();
            //查询商品信息
            foreach (DataRow item in DBHelper.SelectDatatable(sql).Rows)
            {
                Product product = new Product()
                {
                    Address = item["Address"].ToString(),
                    GName = item["GName"].ToString(),
                    ImageUrl = item["ImageUrl"].ToString(),
                    Gid = Convert.ToInt32(item["Gid"].ToString()),
                    Price = Convert.ToDecimal(item["Price"].ToString())

                };

                //查询图片信息
                product.Image = new List<ProductImage>();
                foreach (DataRow img in DBHelper.SelectDatatable(string.Format("SELECT * FROM ProductImage WHERE Gid='{0}'", product.Gid)).Rows)
                {
                    product.Image.Add(new ProductImage { Image = img["Image"].ToString(), Gid = product.Gid, ImgeId = Convert.ToInt32(img["ImgeId"].ToString()) });
                }
                result.Add(product);
            }
            return result;
        }

        /// <summary>
        /// 根据商品类型查找商品
        /// </summary>
        /// <param name="ClassId">商品类型编号</param>
        /// <returns>商品实体集合</returns>
        public static List<Product> SelectProduct(string ClassId) {
            string sql = string.Format("SELECT * FROM Product WHERE ClassID='{0}'",Convert.ToInt32(ClassId));
            List<Product> result = new List<Product>();
            //查询商品信息
            foreach (DataRow item in DBHelper.SelectDatatable(sql).Rows)
            {
                Product product = new Product()
                {
                    Address = item["Address"].ToString(),
                    GName = item["GName"].ToString(),
                    ImageUrl = item["ImageUrl"].ToString(),
                    Gid = Convert.ToInt32(item["Gid"].ToString()),
                    Price = Convert.ToDecimal(item["Price"].ToString())

                };

                //查询图片信息
                product.Image = new List<ProductImage>();
                foreach (DataRow img in DBHelper.SelectDatatable(string.Format("SELECT * FROM ProductImage WHERE Gid='{0}'", product.Gid)).Rows)
                {
                    product.Image.Add(new ProductImage { Image = img["Image"].ToString(), Gid = product.Gid, ImgeId = Convert.ToInt32(img["ImgeId"].ToString()) });
                }
                result.Add(product);
            }
            return result;
        }


    }
}
